﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Customer;
    using UFIDA.U9.ISV.Customer.Proxy;
    using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// CustomerSaveSV partial 
	/// </summary>	
	public partial class CustomerSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new CustomerSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class CustomerSaveSVImpementStrategy : BaseStrategy
	{
		public CustomerSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			CustomerSaveSV bpObj = (CustomerSaveSV)obj;
            try
            {
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");

				List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();
				List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item=>item as object)
					, typeof(UFIDA.U9.ISV.Customer.CustomerDTOData));
				foreach (UFIDA.U9.ISV.Customer.CustomerDTOData dto in inputdtos)
				{
					List<Customer> ret = null;
                    //多组织情况下,需要按照组织+编码形式查找
                    Customer customer = Customer.Finder.Find($"Code='{dto.Code}' and Org='{Context.LoginOrg.ID}'");
					

					if (customer == null)
					{
                        //新增逻辑
                        //BatchCreateCustomerByDTOSVProxy proxy = new BatchCreateCustomerByDTOSVProxy();
                        //proxy.ContextDTO = U9Utils.CreateContext();
                        //proxy.CustomerDTOs = new List<UFIDA.U9.ISV.Customer.CustomerDTOData>() { dto };
                        //List<CustomerData> ret = proxy.Do();

                        ret = CustomerUtils.AITBatchCreateCustomerByDTOSV(new List<UFIDA.U9.ISV.Customer.CustomerDTOData>
                        {
                            dto
                        });
					}
					else
					{
						//修改逻辑
						//BatchModifyCustomerByDTOSVProxy proxy = new BatchModifyCustomerByDTOSVProxy();
						//proxy.ContextDTO = U9Utils.createContext();
						//proxy.CustomerDTOs = new List<UFIDA.U9.ISV.Customer.CustomerDTOData> { dto };
						//List<UFIDA.U9.CBO.SCM.Customer.CustomerData> ret = proxy.Do();

						ret = CustomerUtils.AITBatchModifyCustomerByDTOSV(new List<UFIDA.U9.ISV.Customer.CustomerDTOData>
						{
							dto
						});
					}
					//处理返回
                    if (ret!=null && ret.Count > 0)
                        retlist.Add(new CommonArchiveDataDTOData() { ID = ret[0].ID, Code = ret[0].Code, Name = ret[0].Name });
                }
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch(Exception ex)
            {
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}		
	}

	#endregion
	
	
}